package com.rabbitmq.exec.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import com.rabbitmq.exec.utils.RabbitMqUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description
 * @Author lhj
 * @Date 2024/4/14 17:07
 */
public class ReceiveLogsDirect01 {

    static final Logger logger = LoggerFactory.getLogger(ReceiveLogsDirect01.class);

    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        String queueName = "console";
        channel.queueDeclare(queueName, false, false, false, null);
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        channel.queueBind(queueName, EXCHANGE_NAME, "info");
        channel.queueBind(queueName, EXCHANGE_NAME, "warning");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody());
            logger.info("接收到{}交换机的消息并打印到控制台, 队列: {}, routingKey: {} 消息: {}", EXCHANGE_NAME, queueName, message.getEnvelope().getDeliveryTag(), msg);
        };
        channel.basicConsume(queueName, true, deliverCallback, (consumerTag -> {}));
    }
}
